package com.hd.view.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hd.common.Common;
import com.hd.common.DataPage;
import com.hd.persistence.model.Customer;
import com.hd.persistence.model.Machine;
import com.hd.persistence.model.MachineInfo;
import com.hd.persistence.model.Person;
import com.hd.persistence.model.PurchaseRecord;

/**
 * 交易Action(出售的机器)
 * @author jiaoyang
 *
 */
public class TransactionAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/*list所有客户*/
	private List<Customer> customers;
	/*list所有维修员*/
	private List<Person> maintenanceMen;
	/*list所有销售员*/
	private List<Person> salesperson;
	/*list所有机型*/
	private List<Machine> machines;
	/*购买日期*/
	private String purchaseDate;
	/*安装日期*/
	private String installDate;
	/*合同开始日*/
	private String contractStartDate;
	/*合同到期日*/
	private String contractEndDate;
	/*预警日期*/
	private String warnDate;
	/*保养周期*/
	private String maintainPeriod;
	/*客户*/
	private Customer customer;
	/*客户Id*/
	private Long customerId;
	
	/*出售单*/
	private PurchaseRecord purchaseRecord;
	/*用户够买的机器型号信息*/
	private Machine machine;
	/*用户够买的机器*/
	private MachineInfo machineInfo;
	/*查询出的出售单*/
	private DataPage<PurchaseRecord> purchaseRecords;
	/**
	 * 增加交易记录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addNewTransaction() throws Exception{
		/*列出所有客户*/
		customers = purchaseManager.listCustomers();
		/*列出所有维修员*/
		maintenanceMen = memberManager.listPerson(2,false);//2表示维修人员
		/*列出所有销售员*/
		salesperson =  memberManager.listPerson(-1,false);//非管理员
		/*列出所有机器*/
		machines = memberManager.listMachine();
		return SUCCESS;
	}
	
	/**
	 * 保存购买记录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveNewTransaction() throws Exception{
		customer = purchaseManager.getCustomer(customerId);
		//设置购买日期
		purchaseRecord.setPurchaseDate(Common.String2Date(purchaseDate));
		//设置安装日期
		purchaseRecord.setInstallDate(Common.String2Date(installDate));
		//设置合同开始日
		purchaseRecord.setContractStartDate(Common.String2Date(contractStartDate));
		//设置合同到期日
		purchaseRecord.setContractEndDate(Common.String2Date(contractEndDate));
		//设置预警日期
		purchaseRecord.setWarnDate(Common.String2Date(warnDate));
		
		//设置保养周期
		purchaseRecord.setMaintainPeriod(Integer.parseInt(maintainPeriod));
		
		purchaseRecord.setCustomer(customer);
		purchaseManager.savePurchaseRecord(purchaseRecord);
		
		//获取机型Id
		long machineId = Long.parseLong(purchaseRecord.getMachineModel());
		machine = memberManager.getMachine(machineId);

		machineInfo = new MachineInfo();
		machineInfo.setPurchaseRecord(purchaseRecord);
		machineInfo.setMachineNo(purchaseRecord.getMachineNo());
		machineInfo.setBrand(machine.getBrand());
		machineInfo.setMachineModel(purchaseRecord.getMachineModel());
		machineInfo.setCategory(machine.getCategory());
		machineInfo.setYearsOfLife(machine.getYearsOfLife());
		machineInfo.setSheetyOfLife(machine.getSheetyOfLife());
		//设置鼓
		machineInfo.setCartridgeModel(machine.getCartridgeModel());
		machineInfo.setCartridgeSheety(machine.getCartridgeSheety());
		//设置黑粉
		machineInfo.setBlackPowderModel(machine.getBlackPowderModel());
		machineInfo.setBlackPowderSheety(machine.getBlackPowderSheety());
		//设置彩色粉
		machineInfo.setColorPowderModel(machine.getColorPowderModel());
		machineInfo.setColorPowderSheety(machine.getColorPowderSheety());
		//设置载体
		machineInfo.setCarrierModel(machine.getCarrierModel());
		machineInfo.setCarrierSheety(machine.getCarrierSheety());
		//设置油墨
		machineInfo.setPrintingInkModel(machine.getPrintingInkModel());
		machineInfo.setPrintingInkSheety(machine.getPrintingInkSheety());
		//设置版纸
		machineInfo.setPaperboardModel(machine.getPaperboardModel());
		machineInfo.setPaperboardSheety(machine.getPaperboardSheety());
		//设置炭粉
		machineInfo.setCarbonPowderModel(machine.getCarbonPowderModel());
		machineInfo.setCarbonPowderSheety(machine.getCarbonPowderSheety());
		//第一次购买维修次数设置为0
		machineInfo.setRepairCount(0);
		//保存购买机器的信息
		purchaseManager.saveMachineInfo(machineInfo);
		
		statusTemp = true;
		return SUCCESS;
	}
	
	/**
	 * 查询交易记录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listTransactions() throws Exception{
		/*列出所有客户*/
		customers = purchaseManager.listCustomers();
		/*列出所有维修员*/
		maintenanceMen = memberManager.listPerson(2,false);//2表示维修人员
		/*列出所有机器*/
		machines = memberManager.listMachine();
		
		if(purchaseRecord == null){
			purchaseRecord = new PurchaseRecord();
		}
		purchaseRecords = purchaseManager.pageQueryPurchaseRecords(customerId, Common.String2Date(contractStartDate), Common.String2Date(contractEndDate), purchaseRecord.getMachineModel(), getCurrentPage(), pageSize);
		
		setTotalPage(purchaseRecords.getTotalPageCount());
		return SUCCESS;
	}
	
	/**
	 * 查看交易记录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewTransaction() throws Exception{
		return SUCCESS;
	}


	
/*===================get,set方法===========================================================*/
	public List<Customer> getCustomers() {
		return customers;
	}
	
	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}
	
	public List<Person> getMaintenanceMen() {
		return maintenanceMen;
	}
	
	public void setMaintenanceMen(List<Person> maintenanceMen) {
		this.maintenanceMen = maintenanceMen;
	}

	public List<Person> getSalesperson() {
		return salesperson;
	}

	public void setSalesperson(List<Person> salesperson) {
		this.salesperson = salesperson;
	}

	public PurchaseRecord getPurchaseRecord() {
		return purchaseRecord;
	}

	public void setPurchaseRecord(PurchaseRecord purchaseRecord) {
		this.purchaseRecord = purchaseRecord;
	}

	public String getPurchaseDate() {
		return purchaseDate;
	}

	public void setPurchaseDate(String purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

	public String getInstallDate() {
		return installDate;
	}

	public void setInstallDate(String installDate) {
		this.installDate = installDate;
	}

	public String getContractStartDate() {
		return contractStartDate;
	}

	public void setContractStartDate(String contractStartDate) {
		this.contractStartDate = contractStartDate;
	}

	public String getContractEndDate() {
		return contractEndDate;
	}

	public void setContractEndDate(String contractEndDate) {
		this.contractEndDate = contractEndDate;
	}

	public String getWarnDate() {
		return warnDate;
	}

	public void setWarnDate(String warnDate) {
		this.warnDate = warnDate;
	}

	public String getMaintainPeriod() {
		return maintainPeriod;
	}

	public void setMaintainPeriod(String maintainPeriod) {
		this.maintainPeriod = maintainPeriod;
	}

	public List<Machine> getMachines() {
		return machines;
	}

	public void setMachines(List<Machine> machines) {
		this.machines = machines;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}



	public Long getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Long customerId) {
		this.customerId = customerId;
	}

	public Machine getMachine() {
		return machine;
	}

	public void setMachine(Machine machine) {
		this.machine = machine;
	}

	public MachineInfo getMachineInfo() {
		return machineInfo;
	}

	public void setMachineInfo(MachineInfo machineInfo) {
		this.machineInfo = machineInfo;
	}

	public DataPage<PurchaseRecord> getPurchaseRecords() {
		return purchaseRecords;
	}

	public void setPurchaseRecords(DataPage<PurchaseRecord> purchaseRecords) {
		this.purchaseRecords = purchaseRecords;
	}


}
